
//
// 110402 - AH - (Zen) Created.
// 110403 - AH - toString().
// 110727 - AH - hasNote().
//

package org.soekrat.engine.pit;

import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.soekrat.engine.ontology.OntologyDatastructure;

/**
 * <p>
 *   A node in the
 *   {@link Pit}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
public final class Node 
{
  private final String m_name;
  
  private final List<Content> m_content=new ArrayList<Content>();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param name Name of node.
   */
  public Node(String name)
  {
    if (name==null)
    {
      throw new NullPointerException("name");
    }
    
    m_name=name;
  }
  
  /**
   * <p>
   *   Gets name of node.
   * </p>
   * @return Name.
   */
  public String getName(){ return m_name; }
  
  /**
   * <p>
   *   Adds content.
   * </p>
   * @param content Content to add.
   */
  public void addContent(Content content)
  {
    if (content==null)
    {
      throw new NullPointerException("content");
    }
    
    m_content.add(content);
  }
  
  /**
   * <p>
   *   Removes content.
   * </p>
   * @param content.
   */
  public void removeContent(Content content)
  {
    if (content==null)
    {
      throw new NullPointerException("content");
    }
    
    m_content.remove(content);
  }
  
  /**
   * <p>
   *   Gets all
   *   {@link Content} defined for {@code this} node.
   * </p>
   * @return Content.
   */
  public Content[] getContent()
  { 
    return m_content.toArray(Content.EMPTY_ARRAY); 
  }
  
  /**
   * <p>
   *   Gets 
   *   {@link Content} defined in a given 
   *   {@link OntologyDatastructure}.
   * </p>
   * @param ontology Name ontology in question.
   * @return Content.
   */
  public Content[] getContent(String ontology)
  {
    if (ontology==null)
    {
      throw new NullPointerException("ontology");
    }
    
    List<Content> found=new ArrayList<Content>();
    
    for (Content content : m_content)
    {
      if (content.isDefinedFor(ontology)) found.add(content);
    }
    
    return found.toArray(Content.EMPTY_ARRAY);
  }
  
  /**
   * <p>
   *   Tells if {@code this} has note content or not.
   * </p>
   * @return {@code true} if has, {@code false} if has not.
   */
  public boolean hasNote()
  {
    for (Content curr : m_content) if (curr.isNote()) return true;
    return false;
  }
  
  /**
   * <p>
   *   Gets 
   *   {@link Content} defined in a given 
   *   {@link OntologyDatastructure}.
   * </p>
   * @param ontology {@link OntologyDatastructure} in question.
   * @return Content.
   */
  public Content[] getContent(OntologyDatastructure ontology)
  {
    if (ontology==null)
    {
      throw new NullPointerException("ontology");
    }
    
    List<Content> found=new ArrayList<Content>();
    
    for (Content content : m_content)
    {
      if (content.isDefinedFor(ontology)) found.add(content);
    }
    
    return found.toArray(Content.EMPTY_ARRAY);
  }
  
  @Override
  public String toString() 
  {
    CharArrayWriter caw=new CharArrayWriter();
    PrintWriter pw=new PrintWriter(caw);
    
    pw.println("name="+m_name+", content:");
    for (Content curr : m_content) pw.println("  "+curr);  
    
    pw.flush();
    return caw.toString();
  }
  
}
